package info.xmlprint.ui.widgets;

import info.xmlprint.core.mapping.ElementPath;
import info.xmlprint.core.project.PrintProject;
import info.xmlprint.util.ResizeColumns;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;

public class EditElementPostionComposite extends Composite implements
		IObserverControl, ISetupComposite {
	private ElementAttributTreeViewer attributTreeViewer;
	private ElementPostionTreeViewer positionTreeViewer;

	public EditElementPostionComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout());
		createControls();
	}

	private void createControls() {
		final SashForm sashForm = new SashForm(this, SWT.VERTICAL);
		Composite positionComp = new Composite(sashForm, SWT.NONE);
		positionTreeViewer = new ElementPostionTreeViewer(positionComp,
				SWT.NONE);
		Tree positionTree = positionTreeViewer.getTree();
		addControlListener(new ResizeColumns(positionComp, positionTree));
		positionTree.setLayoutData(new GridData(GridData.FILL_BOTH));

		Composite attributComp = new Composite(sashForm, SWT.NONE);
		attributTreeViewer = new ElementAttributTreeViewer(attributComp,
				SWT.NONE);
		Tree attributTree = attributTreeViewer.getTree();
		addControlListener(new ResizeColumns(attributComp, attributTree));
		attributTree.setLayoutData(new GridData(GridData.FILL_BOTH));
		sashForm.setWeights(new int[] { 1, 1 });
	}

	public void setInput(ElementPath ePath) {
		positionTreeViewer.setInput(ePath);
		if (ePath == null) {
			attributTreeViewer.setInput(null);
		} else {
			attributTreeViewer.setInput(ePath.getCurrPathNode());
		}

		updateControl();
	}

	@Override
	public void setPrintProject(PrintProject pProject) {
		positionTreeViewer.setPrintProject(pProject);
		attributTreeViewer.setPrintProject(pProject);
	}

	@Override
	public void updateControl() {
		positionTreeViewer.updateControl();
		attributTreeViewer.updateControl();
	}

}
